using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace WP.Controls
{
    public partial class TallyText :UserControl 
    {

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        const int splitWidth = 3;
        public enum TallyType { Pc_Len = 2, Pc_Len_Width };

        private TallyType m_TallyStlye = TallyType.Pc_Len_Width;
        private bool m_FixLen = false;
        private bool m_FixWidth = false;
        private bool m_FixPc = false;
        private decimal m_Length =0;
        private decimal m_Width = 0;
        private int  m_Pc = 0;
        private string m_value = "";

        protected System.Windows.Forms.TextBox txtPc;
        protected System.Windows.Forms.TextBox txtLen;
        protected System.Windows.Forms.TextBox txtWid;
       // protected System.Windows.Forms.UserControl  usercontrol;

        public delegate void ValueChangedEventHandler(object sender, TallyEventArgs e);
        public event ValueChangedEventHandler ValueChanged;
        protected virtual void OnValueChanged(TallyEventArgs e)
        {
            if (ValueChanged != null)
            {
                ValueChanged(this, e);
            }
        }
        public TallyText()
        {
            InitializeComponent();
        }

        public TallyType TallyStlye
        {
            get
            {
                return m_TallyStlye;
            }
            set
            {
                m_TallyStlye = value;
                SetTxtWidth();
            }
        }

        private bool IsFixLen
        {
            get { return m_FixLen; }
        }
        private  bool IsFixWidth
        {
            get { return m_FixWidth; }
        }

        private bool IsFixPc
        {
            get {return m_FixPc;} 
        }

        public int TallyFixPc
        {
            get
            {
                return m_Pc;
            }
            set
            {
                m_Pc = value;
                if (m_Pc > 0)
                {
                    m_FixPc = true;
                    txtPc.Enabled = false;
                    txtPc.Text = value.ToString().Trim();
                }
                else
                {
                    m_FixPc = false;
                    txtPc.Enabled = true;
                  //  txtPc.Text = "";
                }
            }

        }
        public decimal TallyFixLength
        {
            get
            {
                return m_Length;
            }
            set 
            {
                m_Length=value ;
                if (m_Length > 0)
                {
                    m_FixLen = true;
                    txtLen.Enabled = false;
                    txtLen.Text = value.ToString().Trim();
                }
                else
                {
                    m_FixLen = false;
                    txtLen.Enabled = true;
                   // txtLen.Text = "";

                }
            }

        }

        public decimal TallyFixWidth
        {
            get
            {
                return m_Width;
            }
            set
            {
                m_Width = value;
                if (m_Width > 0)
                {
                    m_FixWidth = true;
                    this.txtWid.Enabled = false;
                    txtWid.Text = value.ToString().Trim();
                }
                else
                {
                    m_FixWidth = false;
                    this.txtWid.Enabled = true;
                   // txtWid.Text = "";
                }
            }

        }

        public bool ValidTally()
        {
            string[] arrTally;
            
            arrTally = this.Value.Split('/');

            if (this.TallyStlye == TallyType.Pc_Len)
            {

                if (arrTally[0].Trim().Length == 0 || arrTally[1].Trim().Length == 0)
                    return false;
            }
            else
            {
                if (arrTally[0].Trim().Length == 0 || arrTally[1].Trim().Length == 0|| arrTally[2].Trim().Length == 0)
                    return false;
            }
            return true;
        }

        public string Value
        {
            get
            {
                if (this.TallyStlye == TallyType.Pc_Len)
                    m_value = txtPc.Text.Trim() + "/" + txtLen.Text.Trim();
                else
                    m_value = txtPc.Text.Trim() + "/" + txtLen.Text.Trim() + "/" + txtWid.Text.Trim();
                return m_value;
            }
            set 
            {

                string [] arrTally;
                
                this.txtPc.Text = "";
                this.txtLen.Text = "";
                this.txtWid.Text = "";
                if (this.TallyFixLength > 0) this.txtLen.Text = this.TallyFixLength.ToString();
                if (this.TallyFixWidth > 0) this.txtWid.Text = this.TallyFixWidth.ToString();   
                if (value == null)
                {
                    m_value = "";
                    return;
                }
                arrTally = value.Split('/');
                if (this.TallyStlye == TallyType.Pc_Len)
                {
                    if (arrTally.Length != 2 && arrTally.Length != 0)
                    {
                        //throw new InvalidCastException("Must be a tally");
                    }
                    else
                    {
                        if (arrTally.Length ==2)
                        {
                            this.txtPc.Text = arrTally[0];
                            this.txtLen.Text = arrTally[1];  
                         }
  
                    }
                   }
                else
                    {
                        if (arrTally.Length != 3 && arrTally.Length !=0 )
                        {
                            //throw new InvalidCastException("Must be a tally");
                        }
                        else
                        {
                            if (arrTally.Length == 3)
                            {
                                this.txtPc.Text = arrTally[0];
                                this.txtLen.Text = arrTally[1];
                                this.txtWid.Text = arrTally[2];
                            }
                        }
                    }
                
                m_value = value; 
            }
        }

        private void TallyText_Load(object sender, EventArgs e)
        {
            SetTxtWidth();
        }

        public bool CanMoveToPrevTally()
        {
            if (this.ActiveControl.Name == this.txtPc.Name)
            {
                if (this.txtLen.SelectionStart == 0) return true;
            }
            return false;
        }


        public bool CanMoveToNextTally()
        {
            if (this.ActiveControl.Name == this.txtPc.Name || (this.ActiveControl.Name == this.txtLen.Name && this.TallyStlye == TallyType.Pc_Len) || (this.ActiveControl.Name == this.txtWid.Name && this.TallyStlye == TallyType.Pc_Len_Width))
            {
                if (this.ValidTally()) 
                return true;
            }
            return false;
        }



        private void SetTxtWidth()
        {
            int iTop = 0;
            if (this.Parent != null)
            {
                this.Top = (int)((this.Parent.Height - this.Height) / 2m );
            }
           
            
            if (this.TallyStlye == TallyType.Pc_Len)
            {
                this.txtPc.Width = (int)( (this.Width - splitWidth) * 5.0m / (9.0m));
                this.txtLen.Width = (int)(this.Width - splitWidth - txtPc.Width);
            }
            else
            {
                this.txtPc.Width = (int)(( (this.Width - splitWidth * 2)) * 5.0m / (13.0m));
                this.txtLen.Width = (int)(double)(this.Width - splitWidth * 2 - txtPc.Width) / 2;
                this.txtWid.Width = (int)(this.Width - splitWidth * 2 - txtPc.Width - txtLen.Width);
            }
            this.txtPc.Location = new Point(0, iTop);
            this.txtLen.Location = new Point(txtPc.Width + splitWidth, iTop);
            if (this.TallyStlye == TallyType.Pc_Len_Width)
            {
                this.txtWid.Location = new Point(txtLen.Left + txtLen.Width + splitWidth, iTop);
                this.txtWid.Visible = true;
            }
            else
                this.txtWid.Visible = false;
        }



        private void DrowSplitLine()
        {
            Graphics dc = this.CreateGraphics();
            dc.DrawLine(new Pen(Color.Black), new Point(txtPc.Left + txtPc.Width, txtPc.Height), new Point(txtLen.Left, txtLen.Top));
            dc.DrawLine(new Pen(Color.Black), new Point(txtLen.Left + txtLen.Width, txtLen.Height), new Point(txtWid.Left, txtWid.Top));

        }

        private void TallyText_Paint(object sender, PaintEventArgs e)
        {
            DrowSplitLine();
        }

        private void TallyText_SizeChanged(object sender, EventArgs e)
        {
            this.Height = txtPc.Height;
            SetTxtWidth();
            //this.Size = new Size(txtPc.Width + txtLen.Width + txtWid.Width, txtPc.Height);
        }

        private void txtPc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= (char)Keys.D0 && e.KeyChar <= (char)Keys.D9) ||
                (e.KeyChar >= (char)Keys.NumPad0 && e.KeyChar <= (char)Keys.NumPad0) ||
                e.KeyChar == (char)Keys.Back ||  e.KeyChar == '.' || e.KeyChar == (char) Keys.Left  ||  e.KeyChar == (char) Keys.Right   )
                e.Handled = false;
            else
                e.Handled = true;

        }

        private void txtLen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= (char)Keys.D0 && e.KeyChar <= (char)Keys.D9) || 
                (e.KeyChar >= (char)Keys.NumPad0 && e.KeyChar <= (char)Keys.NumPad0) ||
                e.KeyChar == (char)Keys.Back || e.KeyChar == '.' || e.KeyChar == (char)Keys.Left || e.KeyChar == (char)Keys.Right)
                e.Handled = false;
            else
                e.Handled = true;
        }

        private void txtWid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= (char)Keys.D0 && e.KeyChar <= (char)Keys.D9) || 
                (e.KeyChar >= (char)Keys.NumPad0 && e.KeyChar <= (char)Keys.NumPad0) ||
                e.KeyChar == (char)Keys.Back ||  e.KeyChar == '.' || e.KeyChar == (char)Keys.Left || e.KeyChar == (char)Keys.Right)
                e.Handled = false;
            else
                e.Handled = true;
        }

        private void txtPc_KeyDown(object sender, KeyEventArgs e)
        {
            txtPc_KeyDownEvent(e.KeyCode); 
        }


        private void   txtPc_KeyDownEvent(System.Windows.Forms.Keys keyData)
        {
            switch (keyData)
            {

                case Keys.Return:
                    if (this.IsFixLen == false && this.txtLen.CanFocus)
                        txtLen.Focus();
                    break;
                case Keys.Left:
                    //SendKeys.Send("{Left}");
                    break;
                case Keys.Right:
                    if (this.txtPc.SelectionStart == this.txtPc.Text.Length)
                        if (this.IsFixLen == false && this.txtLen.CanFocus)
                            txtLen.Focus();
                    break;
                default:
                    break;
            }

        }



        private void txtLen_KeyDown(object sender, KeyEventArgs e)
        {

            switch (e.KeyCode)
            {

                case Keys.Return:
                    if (this.IsFixWidth == false && this.TallyStlye == TallyType.Pc_Len_Width && this.txtWid.CanFocus)
                        txtWid.Focus();
                    break;
                case Keys.Left:
                    if (this.txtLen.SelectionLength == 0 && this.txtLen.SelectionStart == 0)
                    {
                        if (txtPc.CanFocus) txtPc.Focus();
                    }
                    break;
                case Keys.Right:
                    if (this.txtLen.SelectionStart == this.txtLen.Text.Length)
                        if (this.IsFixWidth == false && this.TallyStlye == TallyType.Pc_Len_Width && this.txtWid.CanFocus)
                            txtWid.Focus();
                    break;
                default:
                    break;
            }

        }

        private void txtWid_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {

                case Keys.Return:
                    break;
                case Keys.Left:
                    if (this.txtWid.SelectionLength == 0 && this.txtWid.SelectionStart == 0)
                    {
                        if (txtLen.CanFocus)
                            txtLen.Focus();
                        else if (txtPc.CanFocus)
                            txtPc.Focus();
                    }
                    break;
                case Keys.Right:
                    break;
                default:
                    break;
            }
        }

        private void txtPc_TextChanged(object sender, EventArgs e)
       {
            OnValueChanged(new TallyEventArgs(this.Value));
        }

        private void txtLen_TextChanged(object sender, EventArgs e)
        {
            OnValueChanged(new TallyEventArgs(this.Value));
        }

        private void txtWid_TextChanged(object sender, EventArgs e)
        {
            OnValueChanged(new TallyEventArgs(this.Value));
        }

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.txtPc = new System.Windows.Forms.TextBox();
            this.txtLen = new System.Windows.Forms.TextBox();
            this.txtWid = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // txtPc
            // 
            this.txtPc.BackColor = System.Drawing.Color.White;
            this.txtPc.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtPc.Location = new System.Drawing.Point(0, 0);
            this.txtPc.Margin = new System.Windows.Forms.Padding(0);
            this.txtPc.Name = "txtPc";
            this.txtPc.Size = new System.Drawing.Size(70, 13);
            this.txtPc.TabIndex = 0;
            this.txtPc.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            this.txtPc.TextChanged += new System.EventHandler(this.txtPc_TextChanged);
            this.txtPc.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtPc_KeyDown);
            this.txtPc.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtPc_KeyPress);
            // 
            // txtLen
            // 
            this.txtLen.BackColor = System.Drawing.Color.White;
            this.txtLen.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtLen.Location = new System.Drawing.Point(76, 0);
            this.txtLen.Margin = new System.Windows.Forms.Padding(0);
            this.txtLen.Name = "txtLen";
            this.txtLen.Size = new System.Drawing.Size(40, 13);
            this.txtLen.TabIndex = 1;
            this.txtLen.TextChanged += new System.EventHandler(this.txtLen_TextChanged);
            this.txtLen.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtLen_KeyDown);
            this.txtLen.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtLen_KeyPress);
            // 
            // txtWid
            // 
            this.txtWid.BackColor = System.Drawing.Color.White;
            this.txtWid.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtWid.Location = new System.Drawing.Point(122, 0);
            this.txtWid.Margin = new System.Windows.Forms.Padding(0);
            this.txtWid.Name = "txtWid";
            this.txtWid.Size = new System.Drawing.Size(40, 13);
            this.txtWid.TabIndex = 2;
            this.txtWid.TextChanged += new System.EventHandler(this.txtWid_TextChanged);
            this.txtWid.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtWid_KeyDown);
            this.txtWid.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtWid_KeyPress);
            // 
            // TallyText
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.White;
            this.Controls.Add(this.txtWid);
            this.Controls.Add(this.txtLen);
            this.Controls.Add(this.txtPc);
            this.Margin = new System.Windows.Forms.Padding(0);
            this.Name = "TallyText";
            this.Size = new System.Drawing.Size(163, 12);
            this.Load += new System.EventHandler(this.TallyText_Load);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.TallyText_Paint);
            this.SizeChanged += new System.EventHandler(this.TallyText_SizeChanged);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion
        public class TallyEventArgs : System.EventArgs
        {
            public TallyEventArgs(string sValue)
            {
                TallyString = sValue;
            }
            public string TallyString;
        }

    }
}
